package com.li.drug.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.li.drug.entity.Billinfo;
import com.li.drug.service.BillinfoService;
import com.li.drug.utils.ResultMapUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

/**
 * 账单信息相关controller
 */
@Controller
@RequestMapping("/billinfo")
public class BillinfoController {

    @Autowired
    BillinfoService billinfoService;

    /**
     * 转向账单信息页面
     * @return
     */
    @RequestMapping
    public String billinfo(){
        return "billinfo/billinfo";
    }

    //分页查询账单信息列表
    @RequestMapping("/billinfoQueryPage")
    @ResponseBody
    public Object billinfoQueryPage(String param,
                                    @RequestParam(name = "page") int pageNum, //page、limit参数是layui传向后台的分页参数
                                    @RequestParam(name = "limit") int pageSize){
        try {
            Page<Billinfo> page = billinfoService.selectBillinfoPage(pageNum,pageSize,param);
            return ResultMapUtil.getHashMapMysqlPage(page);

        }catch (Exception e){
            return ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 转向账单信息新增页面
     * @return
     */
    @RequestMapping("/billinfoPage")
    public String billinfoPage(){
        return "billinfo/billinfoPage";
    }

    /**
     * 添加一个账单信息
     */
    @RequestMapping("/billinfoAdd")
    @ResponseBody
    public Object billinfo(Billinfo billinfo){
        int i = billinfoService.addBillinfo(billinfo);
        return ResultMapUtil.getHashMapSave(i);
    }

    /**
     * 转向账单信息编辑页面
     * @return
     */
    @RequestMapping("/billinfoQueryById")
    public String billinfoEditById(@RequestParam(name = "id") Integer id,
                                   Model model){
        Billinfo billinfo = billinfoService.getBillinfoById(id);
        model.addAttribute("obj",billinfo);
        return "billinfo/billinfoPage";
    }

    /**
     * 修改一个账单信息信息
     */
    @RequestMapping("/billinfoEdit")
    @ResponseBody
    public Object billinfoEdit(Billinfo billinfo){
        try {
            int i = billinfoService.editBillinfo(billinfo);
            return ResultMapUtil.getHashMapUpdate(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 删除一个账单信息信息
     */
    @RequestMapping("/billinfoDelById")
    @ResponseBody
    public Object billinfoDelById(@RequestParam(name = "id") Integer id){
        try {
            int i = billinfoService.delBillinfoById(id);
            return ResultMapUtil.getHashMapDel(i);
        }catch (Exception e){
            return  ResultMapUtil.getHashMapException(e);
        }
    }

    /**
     * 获取所有账单信息列表
     */
    @RequestMapping("/billinfoList")
    @ResponseBody
    public Object billinfoList(){
        List<Billinfo> list = billinfoService.list();
        return list;
    }
}
